/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     progressBar.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-11
 * @brief    This file provides UartTFT Application display function
 ********************************************************************************/
#include "progressBar.h"
#include "bsp.h"
#include "popupBox.h"
#include "levetop.h"
#include "transport.h"
#include "uart.h"
#include "function_drv.h"


/***********************************************************************************
* Function Name	: progress_bar
* Description	: Display progress_bar.
* Input			: - type: progress_bar info sources
				  - buff: progress_bar info
* Output        : None
* Return        : None
************************************************************************************/
void progress_bar(uint8_t type, uint8_t *buff)
{
	uint8_t buff1[12] = {0};
	int16_t sum = 0;
	uint16_t w, h;
	uint16_t dw1 = 0, dh1 = 0, bgw, bgh;
	uint16_t pointer, p_add, x2, y2, x1, y1, bar_id, bg_id;
	int16_t Vmax, Vmin;
	uint8_t dir;

	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8));
		x2 = buff[7] + (buff[8] << 8);
		y2 = buff[9] + (buff[10] << 8);
		bar_id = buff[11] + (buff[12] << 8);
		dir = buff[13];
		Vmax = buff[14] + (buff[15] << 8);
		Vmin = buff[16] + (buff[17] << 8);
		x1 = buff[18] + (buff[19] << 8);
		y1 = buff[20] + (buff[21] << 8);
		bg_id = buff[22] + (buff[23] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		x2 = var[pointer + 3] + (var[pointer + 4] << 8);
		y2 = var[pointer + 5] + (var[pointer + 6] << 8);
		bar_id = var[pointer + 7] + (var[pointer + 8] << 8);
		dir = var[pointer + 9];
		Vmax = var[pointer + 10] + (var[pointer + 11] << 8);
		Vmin = var[pointer + 12] + (var[pointer + 13] << 8);
		x1 = var[pointer + 14] + (var[pointer + 15] << 8);
		y1 = var[pointer + 16] + (var[pointer + 17] << 8);
		bg_id = var[pointer + 18] + (var[pointer + 19] << 8);
	}

		Var_AddrRemapping(&p_add);

	if (Pop_up_flag == 1) // Popup offset
	{
		x1 += Pop_up_x;
		y1 += Pop_up_y;
		x2 += Pop_up_x;
		y2 += Pop_up_y;
	}

	if (bar_id == 0xFFFF)
		return;

	if (bg_id != 0xFFFF)
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * bg_id + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		bgw = buff1[4] + (buff1[5] << 8);
		bgh = buff1[6] + (buff1[7] << 8);
		pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
		pic_flag = buff1[11];

		// progress_bar background
		if (pic_flag == 1 || pic_flag == 2)
		{
			if (pic_update == 0)
				LT268_BG_to_buffer(0, 0, bgw, m_PngData,
									x1, y1, bgw, bgh);
		}
		if (pic_update == 0)
			LT268_pic_to_buffer(0, 0, bgw, m_PngData,
								0, 0, bgw, pic_add,
								bgw, bgh, pic_flag);
		else
			LT268_pic_to_buffer(x1, y1, FullScreen_w, m_PngData,
								0, 0, bgw, pic_add,
								bgw, bgh, pic_flag);
		// progress_bar picture
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * bar_id + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		pic_w = buff1[4] + (buff1[5] << 8);
		pic_h = buff1[6] + (buff1[7] << 8);
		pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
		pic_flag = buff1[11];

		dw1 = pic_w;
		dh1 = pic_h;
		x2 = x2 - x1;
		y2 = y2 - y1;
	}
	else
	{
		// progress_bar coordinate set 0
		x2 = 0;
		y2 = 0;

		// progress_bar picture
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * bar_id + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		pic_w = buff1[4] + (buff1[5] << 8);
		pic_h = buff1[6] + (buff1[7] << 8);
		pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
		pic_flag = buff1[11];

		bgw = pic_w;
		bgh = pic_h;

		dw1 = pic_w;
		dh1 = pic_h;
		if (pic_update == 0)
			LT268_BG_to_buffer(0, 0, bgw, m_PngData,
								x1, y1, bgw, bgh);
	}

	switch (dir)
	{
	case 0: // From left to right

		sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
		if (sum > Vmax)
			sum = Vmax;
		else if (sum < Vmin)
			sum = Vmin;

		w = dw1 * (sum - Vmin) / (Vmax - Vmin);
		if (pic_update == 0)
			LT268_pic_to_buffer(x2, y2, bgw, m_PngData,
								0, 0, dw1, pic_add,
								w, dh1, pic_flag);
		else
			LT268_pic_to_buffer(x2 + x1, y2 + y1, FullScreen_w, m_PngData,
								0, 0, dw1, pic_add,
								w, dh1, pic_flag);

		break;
	case 1: // From right to left

		sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
		if (sum > Vmax)
			sum = Vmax;
		else if (sum < Vmin)
			sum = Vmin;

		w = (dw1) * (sum - Vmin) / (Vmax - Vmin);
		if (pic_update == 0)
			LT268_pic_to_buffer(x2 + dw1 - w, y2, bgw, m_PngData,
								dw1 - w, 0, dw1, pic_add,
								w, dh1, pic_flag);
		else
			LT268_pic_to_buffer(x2 + dw1 - w + x1, y2 + y1, FullScreen_w, m_PngData,
								dw1 - w, 0, dw1, pic_add,
								w, dh1, pic_flag);

		break;
	case 2: // From top to bottom

		sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
		if (sum > Vmax)
			sum = Vmax;
		else if (sum < Vmin)
			sum = Vmin;

		h = dh1 * (sum - Vmin) / (Vmax - Vmin);
		if (pic_update == 0)
			LT268_pic_to_buffer(x2, y2, bgw, m_PngData,
								0, 0, dw1, pic_add,
								dw1, h, pic_flag);
		else
			LT268_pic_to_buffer(x2 + x1, y2 + y1, FullScreen_w, m_PngData,
								0, 0, dw1, pic_add,
								dw1, h, pic_flag);
		break;
	case 3: // From bottom to top

		sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
		if (sum > Vmax)
			sum = Vmax;
		else if (sum < Vmin)
			sum = Vmin;

		h = dh1 * (sum - Vmin) / (Vmax - Vmin);
		//LTPrintf("x2=%d y2=%d \r\n",x2,y2 + dh1 - h);
		if (pic_update == 0)
			LT268_pic_to_buffer(x2, y2 + dh1 - h, bgw, m_PngData,
								0, dh1 - h, dw1, pic_add,
								dw1, h, pic_flag);
		else
			LT268_pic_to_buffer(x2 + x1, y2 + dh1 - h + y1, FullScreen_w, m_PngData,
								0, dh1 - h, dw1, pic_add,
								dw1, h, pic_flag);
		break;
	default:
		break;
	}

	// display
	if (pic_update == 0)
		LT268_buffer_to_TFT(0, 0, bgw, m_PngData,
							x1, y1, bgw, bgh);
}



